package list;

public class MyLinkedList implements IList{
    private ListNode last;
    private ListNode head;

    @Override
    public void addFirst(int data) {
        ListNode newNode = new ListNode(data);
        if(head == null || last == null) {
            head = last = newNode;
            return;
        }
        newNode.next = head;
        head.prev = newNode;
        head = newNode;
    }

    @Override
    public void addLast(int data) {
        ListNode newNode = new ListNode(data);
        if(head == null || last == null) {
            head = last = newNode;
            return;
        }
        newNode.prev = last;
        last.next = newNode;
        last = newNode;
    }

    @Override
    public void addIndex(int index, int data) {
        ListNode cur = head;
        while(cur != null && index-- > 0) {
            cur = cur.next;
        }
        if(index != -1 || cur == null) {
            //抛出异常，下标越界
            return;
        }
        if(cur == head) {
            addFirst(data);
            return;
        }
        ListNode newNode = new ListNode(data);
        newNode.next = cur;
        newNode.prev = cur.prev;
        cur.prev.next = newNode;
        cur.prev = newNode;


    }

    @Override
    public boolean contains(int key) {
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.data == key) {
                if(cur == head) {
                    delHead();
                    return;
                }
                if(cur == last) {
                    delLast();
                    return;
                }
                ListNode curPrev = cur.prev;
                ListNode curNext = cur.next;
                cur.next.prev = curPrev;
                cur.prev.next = curNext;
            }
            cur = cur.next;
        }
        //抛异常 没找到

    }
    private boolean isHeadEqualLast() {
        return head == last;
    }
    private void delHead() {
        if(isHeadEqualLast()) {
            head = last = null;
            return;
        }
        head = head.next;
        head.prev = null;
    }
    private void delLast() {
        if(isHeadEqualLast()) {
            head = last = null;
            return;
        }
        last = last.prev;
        last.next = null;
    }
    @Override
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }
        ListNode cur = head.next;
        //先判断中间的
        while(cur != null && cur.next != null) {
            if(cur.data == key) {
                ListNode curPrev = cur.prev;
                ListNode curNext = cur.next;
                curNext.prev = cur.prev;
                curPrev.next = cur.next;
                cur = curPrev;
            }
            cur = cur.next;
        }
        //检查头部和尾部
        if(head.data == key) {
            delHead();
        }
        if(last.data == key) {
            delLast();
        }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void displayLast() {
        ListNode cur = last;
        while(cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.prev;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        ListNode cur = head;
        ListNode next = cur;
        while(cur != null) {
            next = cur.next;
            cur.prev = cur.next = null;
            cur = next;
        }
        //此时 head 与 last 内部的指针均为 null 但是指向 head 与 last 的指针没有为空
        head = last = null;
    }
}
